# -*- coding: iso-8859-15 -*-

#    SuperClipboard is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import WindowStaticCopy
import WindowNetworkClientsSettings

class WindowSettings(gtk.Window):
    
    def __init__(self,copier):
        gtk.Window.__init__(self,type=gtk.WINDOW_TOPLEVEL)
        self.copier = copier
        self.copier.gui.windows.append(self)
        self.set_title("Settings")
        self.set_icon_from_file(self.copier.config.path['Ico'])
        self.set_resizable(False)
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
        self.connect("delete-event",self.delete_event_callback)
        self.build_window()
        
    def build_window(self): 
        main_vbox = gtk.VBox(False, 15)
        self.add(main_vbox)
        
        globalsettings_frame = gtk.Frame("global settings")
        globalsettings_vbox = gtk.VBox(False,5)
        globalsettings_frame.add(globalsettings_vbox)
        main_vbox.pack_start(globalsettings_frame)
        
        time_hbox = gtk.HBox(False, 5)
        globalsettings_vbox.pack_start(time_hbox)
        time_hbox.pack_start(gtk.Label('Refresh time (ms)'))
        
        
        self.time_spinner = gtk.SpinButton(gtk.Adjustment(self.copier.config.get('Timer'), 100, 5000, 1.0, 100.0, 0.0))
        self.time_spinner.set_wrap(True)
        time_hbox.pack_start(self.time_spinner)

        number_hbox = gtk.HBox(False, 5)
        globalsettings_vbox.pack_start(number_hbox)
        
        number_hbox.pack_start(gtk.Label("Number of elements"))
        
        self.number_spinner = gtk.SpinButton(gtk.Adjustment(self.copier.config.get('Number')+1, 1, 20, 1.0, 1.0, 0.0))
        self.number_spinner.set_wrap(True)
        number_hbox.pack_start(self.number_spinner)
        
        
        parameter_hbox = gtk.HBox(False, 5)
        globalsettings_vbox.pack_start(parameter_hbox)
        
        parameter_hbox.pack_start(gtk.Label("Static copy"))
        
        button_parameter = gtk.Button("Settings")
        button_parameter.connect("clicked",self.parameter_button_callback)
        parameter_hbox.pack_start(button_parameter)
        
        inverse_hbox = gtk.HBox(False, 5)
        globalsettings_vbox.pack_start(inverse_hbox)
        
        self.check = gtk.CheckButton("Inverse click on trayicon")
        self.check.set_active(self.copier.config.get('InverseClick'))
        inverse_hbox.pack_start(self.check)
        

        frame_server = gtk.Frame("Server")
        main_vbox.pack_start(frame_server)
        frame_vbox  = gtk.VBox(False,5)
        frame_server.add(frame_vbox)
        
        server_hbox = gtk.HBox(False, 5)
        frame_vbox.pack_start(server_hbox)
        
        try:
            serverstatus = self.copier.networkserver.status
        except:
            serverstatus = "disconnected"
        frame_vbox.pack_start(gtk.Label('Server Status : '+serverstatus))
        
        self.checkserver = gtk.CheckButton("Active server")
        self.checkserver.set_active(self.copier.config.network['ServerEnabled'])
        self.checkserver.connect("toggled",self.check_server_callback)
        server_hbox.pack_start(self.checkserver)
        
        port_hbox = gtk.HBox(False, 5)
        frame_vbox.pack_start(port_hbox)
        port_hbox.pack_start(gtk.Label("Server Port"))
        
        self.port_spinner = gtk.SpinButton(gtk.Adjustment(self.copier.config.network['ServerPort'], 1, 65635, 1.0, 1.0, 0.0))
        self.port_spinner.set_wrap(True)
        if not self.checkserver.get_active():
            self.port_spinner.set_sensitive(False)
        
        port_hbox.pack_start(self.port_spinner)
        
        
        pass_hbox = gtk.HBox(True, 5)
        frame_vbox.pack_start(pass_hbox)
        pass_hbox.pack_start(gtk.Label("Password"))
        
        self.password = gtk.Entry()
        self.password.set_text(self.copier.config.network['ServerPassword'])
        if not self.checkserver.get_active():
            self.password.set_sensitive(False)
        pass_hbox.pack_start(self.password)
        
        client_hbox = gtk.HBox(False,5)
        client_hbox.pack_start(gtk.Label('Network Clients'))
        
        client_button = gtk.Button("Settings")
        client_button.connect('clicked',self.client_parameter_callback)
        client_hbox.pack_start(client_button)
        
        frame_vbox.pack_start(client_hbox)
        
        validate_hbox= gtk.HBox(False,5)
        main_vbox.pack_start(validate_hbox)
        
        validate_validate_button = gtk.Button('OK')
        validate_validate_button.connect('clicked',self.validate_callback)
        
        validate_cancel_button = gtk.Button('Cancel')
        validate_cancel_button.connect('clicked',self.destroy_callback)
        
        validate_hbox.pack_start(validate_cancel_button)
        validate_hbox.pack_start(validate_validate_button)
        self.show_all()
    

#Callback      
    def validate_callback(self,element=None):
        self.copier.config.set('Number',int(self.number_spinner.get_value()-1))
        self.copier.config.set('Timer',self.time_spinner.get_value())
        self.copier.config.set('InverseClick',self.check.get_active())
        self.copier.config.network['ServerEnabled'] = self.checkserver.get_active()
        self.copier.config.network['ServerPort'] = int(self.port_spinner.get_value())
        self.copier.config.network['ServerPassword'] = self.password.get_text()
        if self.copier.runningNetworkServerThread():
            if not self.copier.config.network['ServerEnabled']:
                self.copier.stopNetworkServerThread()
        else:
            if self.copier.config.network['ServerEnabled']:
                self.copier.startNetworkServerThread()
        self.copier.savesettings()
        self.copier.savenetworksettings()
        
        self.destroy_callback()
        
    def parameter_button_callback(self,element=None):
        if not self.copier.gui.check_opened_window(WindowStaticCopy.WindowStaticCopy):
            WindowStaticCopy.WindowStaticCopy(self.copier)

    def client_parameter_callback(self,element=None):
        if not self.copier.gui.check_opened_window(WindowNetworkClientsSettings.WindowNetworkClientsSettings):
            WindowNetworkClientsSettings.WindowNetworkClientsSettings(self.copier)

    def check_server_callback(self,element=None):
        if not self.checkserver.get_active():
            self.port_spinner.set_sensitive(False)
            self.password.set_sensitive(False)
        else:
            self.port_spinner.set_sensitive(True)
            self.password.set_sensitive(True)
    
    def delete_event_callback(self,*args):
        self.destroy_callback()
    
    def destroy_callback(self,element=None):
        self.copier.gui.windows.remove(self)
        self.destroy()